﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ClearScript.V8;

namespace ICU
{
    class JavaScriptPlugin : Plugin
    {
        V8ScriptEngine engine;
        IServer.Logger logger;
        private bool disposedValue;

        public static string TemplatePlugin { get => "plugin={\nname:\"test\",\nversion:\"1.0.0\",\ndescription:\"just a test\",\ncommands:[],\nauthor:[\"testauthor\"]\n}\n" +
                "function onUserLogin(sender,e){}\n" +
                "function onUserLogout(sender,e){}\n" +
                "function onUserSendMessage(sender,e){}\n" +
                "function onUserSendUserMessage(sender,e){}\n" +
                "function onUserSendGroupMessage(sender,e){}\n" +
                "function onEnable(sender,e){}\n" +
                "function onDisable(sender,e){}\n"; }

        public JavaScriptPlugin(V8ScriptEngine engine, Server server)
        {
            this.engine = engine;
            this.logger = new IServer.Logger(engine.Script.plugin.name);
            server.UserLoginEvent += (sender, e) => { engine.Invoke("onUserLogin", sender, e); };
            server.UserLogoutEvent += (sender, e) => { engine.Invoke("onUserLogout", sender, e); };
            server.UserSendMessageEvent += (sender, e) => { engine.Invoke("onUserSendMessage", sender, e); };
            server.UserSendUserMessageEvent += (sender, e) => { engine.Invoke("onUserSendUserMessage", sender, e); };
            server.UserSendGroupMessageEvent += (sender, e) => { engine.Invoke("onUserSendGroupMessage", sender, e); };
            server.PluginEnableEvent += (sender, e) => { engine.Invoke("onEnable", sender, e); };
            server.PluginDisableEvent += (sender, e) => { engine.Invoke("onDisable", sender, e); };
        }

        event EventHandler<UserSendMessageEventArgs> Plugin.UserSendMessageEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<UserSendGroupMessageEventArgs> Plugin.UserSendGroupMessageEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<UserSendUserMessageEventArgs> Plugin.UserSendUserMessageEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<UserLoginEventArgs> Plugin.UserLoginEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<UserLogoutEventArgs> Plugin.UserLogoutEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<PluginEnableEventArgs> Plugin.PluginEnableEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        event EventHandler<PluginDisableEventArgs> Plugin.PluginDisableEvent
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        public string Name => engine.Script.plugin.name;

        public string Description => engine.Script.plugin.description;

        public string[] Author => engine.Script.plugin.author;

        public Version Version => Version.Parse(engine.Script.plugin.version);

        public string[] Commands => engine.Script.commands;

        public string HomePage => "";

        public string UpdateUrl => "";

        public IServer.Logger GetLogger()
        {
            return logger;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }

                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~JavaScriptPlugin()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}
